package com.example.test1.fragment;


import android.annotation.SuppressLint;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.LinearLayoutManager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.example.test1.Main3Activity;
import com.example.test1.R;
import com.example.test1.adapter.RlvAdapter;
import com.example.test1.adapter.VpAdapter;
import com.example.test1.adapter.VpAdapter2;
import com.example.test1.bean.Channel;
import com.example.test1.http.Api;
import com.example.test1.moudle.Moudle;
import com.example.test1.presenter.Presenter;
import com.example.test1.view.Main_View;
import com.google.gson.Gson;
import com.jcodecraeer.xrecyclerview.XRecyclerView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;
import okhttp3.MediaType;
import okhttp3.RequestBody;

/**
 * A simple {@link Fragment} subclass.
 */
@SuppressLint("ValidFragment")
public class ListsFragment extends Fragment implements Main_View {

    private  Main3Activity Context;
    @BindView(R.id.tab2)
    TabLayout mTab2;
    @BindView(R.id.vp2)
    ViewPager mVp2;
    private View view;
    private Unbinder unbinder;

    public ListsFragment(Main3Activity main3Activity) {
        // Required empty public constructor
        this.Context=main3Activity;
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View inflate = inflater.inflate(R.layout.fragment_lists, container, false);
        data();
        unbinder = ButterKnife.bind(this, inflate);
        return inflate;
    }

    private void data() {
        Presenter presenter = new Presenter(new Moudle(), this);
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/x-www-form-urlencoded,charset-UTF-8"), "");
        presenter.getData("listNewsChannel", requestBody, Api.CHANNEL);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
    }

    @Override
    public void ShowSuccess(Object o, Api api) {
        Log.e("duan", "ShowSuccess: ===>" + o);
        String s = (String) o;
        Gson gson = new Gson();
        Channel channel = gson.fromJson(s, Channel.class);
        List<Channel.DataBean.NewsChannelListBean> titles = channel.getData().getNewsChannelList();
        Log.e("duan", "titles:===== >" +titles);
        ArrayList<Fragment> fragments = new ArrayList<>();
        for (int i = 0; i <titles.size() ; i++) {
            mTab2.setTag(mTab2.newTab().setTag(titles.get(i).getChannelName()));
            fragments.add(new ShowFragment(titles.get(i).getChannelId(),Context));
        }
        VpAdapter2 vpAdapter2 = new VpAdapter2(getChildFragmentManager(),fragments,titles);
        mVp2.setAdapter(vpAdapter2);
        mTab2.setupWithViewPager(mVp2);
    }

    @Override
    public void ShowError(String error) {
        Log.e("duan", "ShowError: ===>" + error);
    }
}
